package com.kaili.basic.controller.web.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseStoreLocationEntity;
import com.kaili.basic.pojo.vo.BaseStoreLocationVO;
import com.kaili.basic.service.BaseStoreLocationService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.*;

/**
 * @author lsw
 * @Type BaseStoreHouse
 * @Desc 杭州凯立通信有限公司Controller
 * @Date 2019年03月07日
 */
@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "库位管理", description = "库位管理")
public class BaseStoreLocationController {
    @Autowired
    private BaseStoreLocationService baseStoreLocationService;

    @GetMapping("/storeLocation/code")
    @ApiOperation(value = "查询库位编号")
    public Long queryStoreLocationCoude(){
        return baseStoreLocationService.queryStoreLocationCoude();
    }

    @GetMapping("/storeLocation")
    @ApiOperation(value = "分页查询库位")
    public Page<BaseStoreLocationEntity> queryPageList(@ModelAttribute BaseStoreLocationQueryDto queryDto){
        return baseStoreLocationService.queryPageList(new Page<BaseStoreLocationEntity>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/storeLocation/byType")
    @ApiOperation(value = "通过类型查询库位")
    public List<BaseStoreLocationEntity> queryLocationByType(@ModelAttribute BaseStoreLocationQueryDto queryDto){
        return baseStoreLocationService.queryPageList(queryDto);
    }

    @GetMapping("/storeLocation/export")
    @ApiOperation(value = "库位导出")
    public void storeLocationExport(HttpServletResponse response, @ModelAttribute BaseStoreLocationQueryDto queryDto) throws RestException {
        List<BaseStoreLocationVO> list = baseStoreLocationService.storeLocationExport(queryDto);
        ExcelUtil.exportExcel(list,"库位信息","sheet1", BaseStoreLocationVO.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/storeLocation")
    @ApiOperation(value = "新增库位")
    public RestStatus addStoreLocation(@RequestBody BaseStoreLocationAddDto addDto){
        BaseStoreLocationEntity entity=new BaseStoreLocationEntity();
        entity.setCreateTime(new Date());
        entity.setCreateBy(2);
        BeanUtils.copyProperties(addDto,entity);
        baseStoreLocationService.insert(entity);
        return CommonStatus.ADD_OK;
    }

    @PutMapping("/storeLocation")
    @ApiOperation(value = "修改库位")
    public RestStatus updateStoreLocation(@RequestBody BaseStoreLocationAddDto addDto){
        BaseStoreLocationEntity entity=new BaseStoreLocationEntity();
        entity.setModifyTime(new Date());
        entity.setModifyBy(2);
        BeanUtils.copyProperties(addDto,entity);
        baseStoreLocationService.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }

    @PutMapping("/storeLocation/status")
    @ApiOperation(value = "修改库位状态")
    public RestStatus updateLocationStatus(@RequestBody BaseStoreLocationEditDto dto){
        BaseStoreLocationEntity entity=new BaseStoreLocationEntity();
        entity.setId(dto.getId());
        entity.setStatus(dto.getStatus());
        baseStoreLocationService.updateById(entity);
        return CommonStatus.UPDATE_OK;
    }

    @PutMapping("/storeLocation/default")
    @ApiOperation(value = "设置默认库位")
    public RestStatus updateLocationDefault(@RequestBody BaseStoreLocationEditDto dto){
        if(dto.getStatus()==0){
            BaseStoreLocationEntity entity = baseStoreLocationService.selectById(dto.getId());
            baseStoreLocationService.updateDefaultLocation(new HashMap<String,Object>(){{put("defaultLocation",1);
                put("store_id",entity.getStoreId());
                put("storeLocationType",entity.getStoreLocationType());
            }});
        }
        Map<String,Object> map=new HashMap<>();
        map.put("id",dto.getId());
        map.put("defaultLocation",dto.getStatus());
        baseStoreLocationService.updateDefaultLocation(map);
        return CommonStatus.UPDATE_OK;
    }

    @DeleteMapping("/storeLocation/{id}")
    @ApiOperation(value = "删除库位")
    public RestStatus delStoreLocation(@PathVariable("id") Integer id){
        baseStoreLocationService.deleteById(id);
        return CommonStatus.DELETE_OK;
    }

}
